import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/automationRule'

const api = {

    /**
     * 新增自动化规则
     * @param automationRule
     * @returns {Promise<*|undefined>}
     */
    createAutomationRule: (automationRule) =>
      axiosClient().post(`${requestUrl}/addInfo`, automationRule),

    /**
     * 根据ID删除自动化规则
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteAutomationRuleById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新自动化规则
     * @param id
     * @param automationRule
     * @returns {Promise<*|undefined>}
     */
    updateAutomationRuleFull: (id, automationRule) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, automationRule),

    /**
     * 根据ID部分更新自动化规则
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateAutomationRulePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询自动化规则
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getAutomationRuleById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询自动化规则
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getAutomationRulePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询自动化规则
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getAutomationRuleList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.nodeId) {
            params.append('nodeId', businessParam.nodeId)
        }
        if (businessParam.ruleCode) {
            params.append('ruleCode', businessParam.ruleCode)
        }
        if (businessParam.ruleName) {
            params.append('ruleName', businessParam.ruleName)
        }
        if (businessParam.conditionExpr) {
            params.append('conditionExpr', businessParam.conditionExpr)
        }
        if (businessParam.executionType) {
            params.append('executionType', businessParam.executionType)
        }
        if (businessParam.executionParam) {
            params.append('executionParam', businessParam.executionParam)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
